package com.itc.canvas;

import java.util.Random;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import com.itc.display.Main;
import com.itc.resource.LoadResource;
import com.itc.score.HighScore;

public class ResultUICanvas extends GameCanvas implements CommandListener {
	private Main midlet;
	private int width;
	private int height;
	private int R, G, B;
	private Random rnd;
	private Command backCmd;
	private HighScore highScore = new HighScore();

	protected ResultUICanvas(Main midlet) {
		super(true);
		this.midlet = midlet;
		initViews();
		initProperties();
		initListener();
	}

	public void initProperties() {
		rnd = new Random();
	}

	public void initListener() {
		addCommand(backCmd);
		setCommandListener(this);
	}

	public void initViews() {
		width = getWidth();
		height = getHeight();
		backCmd = new Command("Back", Command.BACK, 1);
	}

	public void paint(Graphics g) {
		R = LoadResource.GRB[rnd.nextInt(4)];
		G = LoadResource.GRB[rnd.nextInt(4)];
		B = LoadResource.GRB[rnd.nextInt(4)];
		g.setColor(R, G, B);
		g.fillRect(0, 0, width, height);
		g.drawImage(LoadResource.mImgBackgroundScore, (width - 200) / 2,
				(height - 141) / 2, Graphics.LEFT | Graphics.TOP);
		// draw play
		g.drawImage(LoadResource.mImgPlay, width / 2 - 90,
				150 + (height - 141) / 2, Graphics.LEFT | Graphics.TOP);
		// draw menu
		g.drawImage(LoadResource.mImgMenu, width / 2 + 10,
				150 + (height - 141) / 2, Graphics.LEFT | Graphics.TOP);
		// draw score current
		drawScore(g);
	}

	public void drawScore(Graphics g) {
		highScore.getScore();
		// print score
		// printScore(g, highScore.getScore(), 135);
		int score = highScore.getScore();
		// print best score
		try {
			HighScore.readHighScore();
		} catch (Exception e) {
		}
		int bestScore = highScore.getBest();
		// printScore(g, score, 135);
		if (score > bestScore) {
			bestScore = score;
			try {
				HighScore.writeHighScore(bestScore + "");
			} catch (Exception e) {
			}
		}
		// printScore(g, bestScore, 180);
		Font font = g.getFont();
		// int mSizeText = font.stringWidth("Score: " +
		// Score.getCurrentScore());
		g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC
				| Font.STYLE_BOLD, Font.SIZE_LARGE));
		g.setColor(0xffffff);
		g.drawString(score + "", (width - 200) / 2 + 110,
				(height - 141) / 2 + 52, Graphics.LEFT | Graphics.TOP);
		g.drawString(bestScore + "", (width - 200) / 2 + 110,
				(height - 141) / 2 + 92, Graphics.LEFT | Graphics.TOP);
	}

	protected void pointerPressed(int x, int y) {
		// button menu
		if (x >= width / 2 + 10 && y >= 150 + (height - 141) / 2
				&& x <= width / 2 + 10 + 80
				&& y <= 150 + (height - 141) / 2 + 40) {
			showMenuUI();
		}
		// button play
		if (x >= width / 2 - 90 && y >= 150 + (height - 141) / 2
				&& x <= width / 2 - 90 + 80
				&& y <= 150 + (height - 141) / 2 + 40) {
			showPlayUI();
		}
	}

	public void showMenuUI() {
		Display display = Display.getDisplay(this.midlet);
		MenuCanvas menuCanvas = new MenuCanvas(this.midlet);
		display.setCurrent(menuCanvas);
	}

	public void showPlayUI() {
		Display display = Display.getDisplay(this.midlet);
		GameUICanvas gameUICanvas = new GameUICanvas(this.midlet);
		display.setCurrent(gameUICanvas);
	}

	public void commandAction(Command c, Displayable d) {
		if (c == backCmd) {
			showMenuUI();
		}
	}

}
